#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: hospital
    Description:
        Author: lhw
        Date: 2020/6/2
-----------------------------------
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from EducationContent import models
from EducationContent.serializers.hospital import HospitalModelSerializer
from Common.logger import app_log
from Common.response import resp
from rest_framework.exceptions import ValidationError
from Admin.models import AdminUser


class HospitalView(APIView):
    authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            user_email = request.user["data"]["email"]
            hospital = AdminUser.objects.filter(email=user_email).values("hospital").first().get("hospital", "")
            if hospital == "ALL":
                hospital_obj = models.Hospital.objects.filter(is_delete=False).all()
            else:
                hospital_obj = models.Hospital.objects.filter(hospital_name=hospital, is_delete=False).all()
            hospital_data = HospitalModelSerializer(hospital_obj, many=True).data
            return Response({
                'error_code': 0,
                'message': "Success",
                'data': hospital_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            user_email = request.user["data"]["email"]
            role = AdminUser.objects.filter(email=user_email).values("role__title").first().get("role__title", "")
            if role != "Superuser":
                return resp(1005, {})
            request_data = request.data
            hospital_ser = HospitalModelSerializer(data=request_data)
            hospital_ser.is_valid(raise_exception=True)
            hospital_obj = hospital_ser.save()
            return Response({
                'error_code': 0,
                'message': "Success",
                'data': HospitalModelSerializer(hospital_obj).data
            })
        except ValidationError as e:
            app_log.error(e)
            return resp(4007, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def patch(self, request, *args, **kwargs):
        try:
            user_email = request.user["data"]["email"]
            role = AdminUser.objects.filter(email=user_email).values("role__title").first().get("role__title", "")
            if role != "Superuser":
                return resp(1005, {})
            id = request.data.get("hospital_id", "")
            hospital_name = request.data.get("hospital_name", "")
            if models.Hospital.objects.filter(hospital_name=hospital_name, is_delete=False).exclude(id=id):
                return resp(4007, {})
            models.Hospital.objects.filter(id=id).update(hospital_name=hospital_name)
            hospital_logo = request.data.get("hospital_logo", "")
            if hospital_logo:
                models.Hospital.objects.filter(id=id).update(hospital_logo=hospital_logo)

            return Response({
                'error_code': 0,
                'message': "Change hospital_name Success"
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            user_email = request.user["data"]["email"]
            role = AdminUser.objects.filter(email=user_email).values("role__title").first().get("role__title", "")
            if role != "Superuser":
                return resp(1005, {})
            hospital_id = request.GET.get("hospital_id", "")
            models.Hospital.objects.filter(id=hospital_id, is_delete=False).update(is_delete=True)
            models.EducationCategory.objects.filter(hospital_id=hospital_id).delete()
            # models.EducationFeeds.objects.filter(hospital_id=hospital_id, is_delete=False).update(is_delete=True)
            models.EducationFeeds.objects.filter(hospital_id=hospital_id, is_delete=False).delete()
            return Response({
                'error_code': 0,
                'message': "Delete Success"
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
